package cn.zhqr.lib.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by charles on 2018/12/4.
 */
public abstract class Dict {
    private static final Map<Class,List<KeyValue>> pairs_pool = new HashMap<>();

//    private static final Map<Class,Dict> dict_instance_pool = new HashMap<>();

    private static final String system_default_value = "未知";

    public static KeyValue add(Object key,String value,Class clazz){
        KeyValue pair = new KeyValue(key, value);

        List<KeyValue> pairs = pairs_pool.get(clazz);
        if(pairs==null){
            pairs = new ArrayList<>();
        }
        pairs.add(pair);
        pairs_pool.put(clazz,pairs);
        return pair;
    }
    public static List keys(Class<? extends Dict> clazz){
        initial(clazz);
        List<KeyValue> pairs = pairs_pool.get(clazz);

        List keyList = new ArrayList<>();
        if(pairs!=null){
            for(KeyValue p : pairs){
                keyList.add(p.key);
            }
        }
        return keyList;
    }
    public static List values(Class<? extends Dict> clazz){
        initial(clazz);
        List<KeyValue> pairs = pairs_pool.get(clazz);

        List<String> valueList = new ArrayList<>();
        if(pairs!=null){
            for(KeyValue p : pairs){
                valueList.add(p.value);
            }
        }
        return valueList;
    }

    public static List<KeyValue> keyValueList(Class<? extends Dict> clazz){
        initial(clazz);
        return pairs_pool.get(clazz);
    }

    public static String value(Object key,Class<? extends Dict> clazz,String default_value){
        initial(clazz);

        List<KeyValue> pairs = pairs_pool.get(clazz);
        if(pairs!=null){
            for(KeyValue pair : pairs){
                if(pair.key.equals(key)){
                    return pair.value;
                }
            }
        }
        return default_value;
    }

    public static void initial(Class<? extends  Dict> clazz){
        try {
            Class.forName(clazz.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static String value(Object key,Class<? extends Dict> clazz){
        return value(key,clazz,system_default_value);
    }
}
